package com.wongoing.webchat.pub.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.junit.BeforeClass;
import org.junit.Test;

import com.qq.weixin.mp.aes.AesException;
import com.qq.weixin.mp.aes.WXBizMsgCrypt;
import com.wongoing.api.wechat.pub.WebChat;
import com.wongoing.api.wechat.pub.entity.AccessToken;
import com.wongoing.api.wechat.pub.entity.Menu;
import com.wongoing.api.wechat.pub.entity.MenuParam;
import com.wongoing.api.wechat.pub.entity.MenuType;
import com.wongoing.api.wechat.pub.entity.MsgType;
import com.wongoing.api.wechat.pub.entity.ResultMsg;
import com.wongoing.api.wechat.pub.entity.SendMsg;
import com.wongoing.api.wechat.pub.entity.TextMsg;
import com.wongoing.api.wechat.pub.entity.UserInfo;
import com.wongoing.apiclient.weixin.WXPayUtil;
import com.wongoing.sys.shiro.ShiroHelper;

public class PubWebChatTester {
	/**
	 * 微信公众号开发者ID-易飞出行
	 */
	private static final String AppID = "wxbc533a2088ab9ba6";
	/**
	 * 微信公众号开发者密码-易飞出行
	 */
	private static final String AppSecret = "66ee0c691589fbc530799cae59c34fbd";
	
	private static AccessToken accessToken = null;
	
	@BeforeClass
	public static void beforeClass() throws IOException {
		System.out.println("before");
		accessToken = WebChat.getAccessTokenEntity(AppID, AppSecret);
	}
	
	/**
	 * 功能说明：获取AccessToken测试
	 * 修改说明：
	 * @author zhenglibing
	 * @date 2018年1月10日 下午4:11:10
	 */
	@Test
	public void testGetAccessToken() {
		System.out.println("access_token=" + accessToken.getAccess_token());
		System.out.println("expires_in=" + accessToken.getExpires_in());
	}
	
	@Test
	public void testVerifyUrl() throws AesException {
		
		String token = "zhipinji";
		String sEncodingASEKey = "xQyVqs3HwbHXLzdlo99BGhcmimFnM3p4E0ngxUKueIM";
		
//		String signature = "842216fbf947ab707894da3385b8b9036b1ddeb8";
//		String timestamp = "1515980960";
//		String nonce = "2483648106";
//		String echostr = "12039234876325411816";
		
		String signature = "c57d9ce45b3cd3ec789dddd5b37909f4ca9d10e8";
		String timestamp = "1515985518";
		String nonce = "2794530924";
		String echostr = "11085091557833817983";
		
		WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(token, sEncodingASEKey, AppID);
		String vechoStr = wxcpt.verifyUrl(signature, timestamp, nonce, echostr);
		System.out.println(vechoStr);
	}
	
	/**
	 * 功能说明：发送文本消息测试
	 * 修改说明：
	 * @author zhenglibing
	 * @date 2018年1月10日 下午4:10:57
	 * @throws IOException
	 */
	@Test
	public void testSendTextMsg1() throws IOException {
		SendMsg sendMsg = new SendMsg();
		sendMsg.setTouser("oR66g1JUHw5Yjz4fGzg26WhVVATE");
		sendMsg.setMsgtype(MsgType.Text);
		sendMsg.setText(new TextMsg());
		sendMsg.getText().setContent("消息测试 for java2");
		
//		String jsonResult = WebChat.sendTextMsg1(accessToken.getAccess_token(), sendMsg.toJsonString());
//		System.out.println(jsonResult);
		
		ResultMsg result = WebChat.sendTextMsg(accessToken.getAccess_token(), sendMsg.toJsonString());
		System.out.println("errcode=" + result.getErrcode());
		System.out.println("errmsg=" + result.getErrmsg());
	}
	
	/**
	 * 功能说明：创建微信菜单测试
	 * 修改说明：
	 * @author zhenglibing
	 * @date 2018年1月10日 下午4:10:39
	 * @throws IOException
	 */
	@Test
	public void testCreateMenuInfo() throws IOException {
		String redirect_url = "http://web.zhipinji.com/webchat/pub/link";	//回调地址
		//静默授权url
		String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_base&state=%s#wechat_redirect";
		//state为对应菜单进入的页面
		
		MenuParam menuParam = new MenuParam();
		
		//出行伴旅
		Menu mnu1 = new Menu();
		mnu1.setType(MenuType.Click);
		mnu1.setKey("KEY-ZPJ");
		mnu1.setName("直拼机");
		if (null == mnu1.getSub_button())
		{
			mnu1.setSub_button(new ArrayList<Menu>());
		}
		
		//出行伴旅-休息室
		Menu mnu11 = new Menu();
		mnu11.setType(MenuType.View);
		mnu11.setKey("KEY-group");
		mnu11.setName("拼团首页");
		mnu11.setUrl(String.format(url, AppID, redirect_url, "home"));
		mnu1.getSub_button().add(mnu11);
		
		//出行伴旅-礼宾车
		Menu mnu12 = new Menu();
		mnu12.setType(MenuType.View);
		mnu12.setKey("KEY-release");
		mnu12.setName("发布需求");
		mnu12.setUrl(String.format(url, AppID, redirect_url, "release"));
		mnu1.getSub_button().add(mnu12);
		
		//出行伴旅-机票预订
		Menu mnu13 = new Menu();
		mnu13.setType(MenuType.View);
		mnu13.setKey("KEY-demand");
		mnu13.setName("我的需求");
		mnu13.setUrl(String.format(url, AppID, redirect_url, "demand"));
		mnu1.getSub_button().add(mnu13);
		
		//出行伴旅-航班动态
		Menu mnu14 = new Menu();
		mnu14.setType(MenuType.View);
		mnu14.setKey("KEY-myGroup");
		mnu14.setName("我的拼团");
		mnu14.setUrl(String.format(url, AppID, redirect_url, "group"));
		mnu1.getSub_button().add(mnu14);
	
		
		//企业资讯
		Menu mnu2 = new Menu();
		mnu2.setType(MenuType.Click);
		mnu2.setKey("KEY-QYZX");
		mnu2.setName("企业资讯");
		if (null == mnu2.getSub_button()) {
			mnu2.setSub_button(new ArrayList<Menu>());
		}
		
		//企业资讯-官网
//		Menu mnu21 = new Menu();
//		mnu21.setType(MenuType.View);
//		mnu21.setKey("KEY-index");
//		mnu21.setName("官网");
//		mnu21.setUrl(String.format(url, AppID, redirect_url, "index"));
//		mnu2.getSub_button().add(mnu21);
		
		//企业资讯-微博
		Menu mnu22 = new Menu();
		mnu22.setType(MenuType.View);
		mnu22.setKey("KEY-weibo");
		mnu22.setName("微博");
		mnu22.setUrl("https://m.weibo.cn/p/1005056440613298");
		mnu2.getSub_button().add(mnu22);
		
		//企业资讯-企业文化
		Menu mnu23 = new Menu();
		mnu23.setType(MenuType.View);
		mnu23.setKey("KEY-culture");
		mnu23.setName("企业文化");
		mnu23.setUrl(String.format(url, AppID, redirect_url, "culture"));
		mnu2.getSub_button().add(mnu23);
		
		//我的
		Menu mnu3 = new Menu();
		mnu3.setType(MenuType.Click);
		mnu3.setKey("KEY-WD");
		mnu3.setName("我的");
		if (null == mnu3.getSub_button()) {
			mnu3.setSub_button(new ArrayList<Menu>());
		}
		
		//我的-我的账户
		Menu mnu31 = new Menu();
		mnu31.setType(MenuType.View);
		mnu31.setKey("KEY-my");
		mnu31.setName("我的");
		mnu31.setUrl(String.format(url, AppID, redirect_url, "my"));
		mnu3.getSub_button().add(mnu31);
		
		//我的-我的订单
		Menu mnu33 = new Menu();
		mnu33.setType(MenuType.View);
		mnu33.setKey("KEY-order");
		mnu33.setName("我的订单");
		mnu33.setUrl(String.format(url, AppID, redirect_url, "my/page/myOrder"));
		mnu3.getSub_button().add(mnu33);
		
		//我的-APP下载
		Menu mnu34 = new Menu();
		mnu34.setType(MenuType.View);
		mnu34.setKey("KEY-app");
		mnu34.setName("APP下载");
		mnu34.setUrl(String.format(url, AppID, redirect_url, "app"));
		mnu3.getSub_button().add(mnu34);
		
		menuParam.add(mnu1);
		menuParam.add(mnu2);
		menuParam.add(mnu3);
		
		ResultMsg result = WebChat.createMenu(accessToken.getAccess_token(), menuParam);
		
		System.out.println(result.getErrcode());
		System.out.println(result.getErrmsg());
	}
	
	/**
	 * 功能说明：获取微信菜单信息测试
	 * 修改说明：
	 * @author zhenglibing
	 * @date 2018年1月10日 下午4:11:29
	 * @throws IOException
	 */
	@Test
	public void testGetMenuInfo() throws IOException {
		String menuinfo = WebChat.getMenu(accessToken.getAccess_token());
		System.out.println(menuinfo);
	}
	
	/**
	 * 功能说明：获取微信用户列表测试
	 * 修改说明：
	 * @author zhenglibing
	 * @date 2018年1月10日 下午4:11:41
	 * @throws IOException
	 */
	@Test
	public void testGetUser() throws IOException {
		ResultMsg result = WebChat.getUser(accessToken.getAccess_token(), "");
		System.out.println("total=" + result.getTotal());
		System.out.println("count=" + result.getCount());
		for(String openid : result.getData().getOpenid()) {
			System.out.println(openid);
		}
		System.out.println("next_openid=" + result.getNext_openid());
	}
	
	/**
	 * 功能说明：获取微信用户信息测试
	 * 修改说明：
	 * @author zhenglibing
	 * @date 2018年1月10日 下午4:12:04
	 * @throws IOException
	 */
	@Test
	public void testGetUserInfo() throws IOException {
		//String openId="oJlBswv8cVlgENs9lPK7dCyQEXzk";
		String openId="oC7XR1VCXl9PNFZjg2aCrHbHdhu4";
		UserInfo result = WebChat.getUserInfo(accessToken.getAccess_token(), openId);
		System.out.println("subscribe=" + result.getSubscribe());
		System.out.println("openid=" + result.getOpenid());
		System.out.println("nickname=" + result.getNickname());
		System.out.println("sex=" + result.getSex());
		System.out.println("city=" + result.getCity());
		System.out.println("country=" + result.getCountry());
		System.out.println("province=" + result.getProvince());
		System.out.println("language=" + result.getLanguage());
		System.out.println("headimgurl=" + result.getHeadimgurl());
		System.out.println("subscribe_time=" + result.getSubscribe_time());
		System.out.println("unionid=" + result.getUnionid());
		System.out.println("remark=" + result.getRemark());
		System.out.println("groupid=" + result.getGroupid());
		System.out.print("tagid_list=");
		if (result.getTagid_list() != null) {
			for(int tagid : result.getTagid_list()) {
				System.out.print(tagid);
				System.out.print(",");
			}
			System.out.println();
		}
	}
	
	@Test
	public void testGetTimestamp() {
		System.out.println(WebChat.getTimestamp());
	}
	
	@Test
	public void testMD5() throws Exception {
		System.out.println(WXPayUtil.MD5("admin"));
		System.out.println(ShiroHelper.md5Encrypt("admin", "", "", 1));
		
	}
}
